package com.dhp.Utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.dhp.constant.FileConstant;
import org.springframework.web.multipart.MultipartFile;

public class UploadUtil {

    public static boolean CheckFile(MultipartFile file){
        if (file == null){
            return false;
        }

        //检验文件的格式是否正确
        String fileName = file.getOriginalFilename();
        if (StrUtil.isEmpty(fileName)){
            return false;
        }
        String suffix = FileUtil.getSuffix(fileName);
        if (!suffix.equals(FileConstant.XLS) && !suffix.equals(FileConstant.XLSX)){
            return false;
        }

        //校验文件的大小
        long size = file.getSize();
        if (size > FileConstant.MAX_SIZE){
            return false;
        }
        return true;
    }
}
